﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Home_Reports : System.Web.UI.Page
{
    public CategoryAdapter cateAdap;
    private PlatformAdapter platformAdap;
    public List<PlatformEntity> platforms { set; get; }
    public List<CategoryEntity> categories { set; get; }
    public BookAdapter bookAdap;
    UserEntity currentUser;
    public LanguageManage Language;
    public string title = "TopDownload";
    public long categoryID;
    public long reportid;
    protected void Page_Load(object sender, EventArgs e)
    {
        //Check Login
        currentUser = UserManager.Instance.Getuser();
        if (!currentUser.isLogin)
            Response.Redirect("~/Login.aspx");

        Language = new LanguageManage();
        cateAdap = new CategoryAdapter();
        bookAdap = new BookAdapter();
        platformAdap = new PlatformAdapter();
        Session["menu_id"] = "reports";
        if (!IsPostBack)
        {
            if (Request.QueryString["reportid"] != null)
            {
                Session["menu_current"] = Session["menu_id"];
                Session["reportid"] = Convert.ToInt32(Request.QueryString["reportid"]);
                categories = cateAdap.getListCategories();
                //
                reportid = Convert.ToInt32(Request.QueryString["reportid"]);
                if (reportid == 1 || reportid==2)
                {
                    //load categories for combobox                    
                    comboCategories.DataSource = categories;
                    comboCategories.DataTextField = "Name";
                    comboCategories.DataValueField = "ID";
                    comboCategories.DataBind();
                    if (reportid == 1)
                    {
                        Report1.reportid = 1;
                        title = "TopDownload";
                    }
                    else
                    {
                        Report1.reportid = 2;
                        title = "TopLike";
                    }
                    if (Session["categoriesindex"] != null)
                        comboCategories.SelectedIndex = (int)Session["categoriesindex"];
                }                
                else
                {
                    //load categories for combobox                    
                    comboCategories2.DataSource = categories;
                    comboCategories2.DataTextField = "Name";
                    comboCategories2.DataValueField = "ID";
                    comboCategories2.DataBind();
                    Report1.reportid = 3;
                    title = "Statistics";
                    if (Session["categoriesindex"] != null)
                        comboCategories2.SelectedIndex = (int)Session["categoriesindex"];                   
                    platforms = platformAdap.getListPlatform();
                }               
                if (Session["categories"] != null)
                {
                    Report1.listBook = (List<BookEntity>)Session["categories"];
                }
                else
                {
                    categoryID = categories[0].ID;
                    Session["categories"] = bookAdap.getListBook(categoryID, currentUser);
                    Report1.listBook = (List<BookEntity>)Session["categories"];
                }    
            }
        }
    }
    protected void comboCategories_SelectedIndexChanged(object sender, EventArgs e)
    {
        reportid = Convert.ToInt32(Request.QueryString["reportid"]);
        categoryID = Convert.ToInt32(comboCategories.SelectedValue);
        Session["categories"] = bookAdap.getListBook(categoryID, currentUser);
        Session["categoriesindex"] = comboCategories.SelectedIndex;
        Response.Redirect("~/Home/Reports.aspx?reportid=" + reportid);
    }
    protected void comboCategories2_SelectedIndexChanged(object sender, EventArgs e)
    {
        
        
        
    }
    protected void btnFilter_Click(object sender, EventArgs e)
    {
        categoryID = Convert.ToInt32(comboCategories2.SelectedValue);
        List<BookDetailEntity> Details = new List<BookDetailEntity>();
        platforms = platformAdap.getListPlatform();
        int cout = platforms.Count();
        for (int i = 0; i < platforms.Count(); i++)
        {
            if (Request.Form["platform_" + platforms[i].ID] != null)
            {
                BookDetailEntity detail = new BookDetailEntity();
                detail.PlatformID = platforms[i].ID;
                Details.Add(detail);

            }
        }
        Session["categories"] = bookAdap.getListBook(categoryID,Details, currentUser);
        Session["categoriesindex"] = comboCategories2.SelectedIndex;
        Response.Redirect("~/Home/Reports.aspx?reportid=" + reportid);
    }
}
